<?php

namespace app\api\model;

use think\Model;
use app\common\model\Comments;

class Comments extends Comments
{
    public function users()
    {
        return $this->belongsTo('users', 'user_id');
    }
    /**
     * 创建评论
     * @param  [type] $user_info  [description]
     * @param  [type] $body     [description]
     * @param  [type] $reply_id [description]
     * @return [type]           [description]
     */
    public function add($user_info, $body, $reply_id = null)
    {
    	$comment_info = [
    		'user_id' => $user_info->id,
    		'body'    => $body,
            'reply_id' => $reply_id
    		//''
    	];
    	return $this->commentCreate($comment_info);
    }
    /**
     * 评论回复
     * @param  [type] $comment_info [description]
     * @return [type]               [description]
     */
    public function reply($user_info, $body, $reply_id)
    {
    	return $this->add($user_info, $body, $reply_id);
    }
    /**
     * [read description]
     * @param  [type] $comment_id [description]
     * @return [type]             [description]
     */
    public function read($comment_id)
    {
        $comment = $this->get($comment_id);
        $replys = count($this->replyLists($comment));
        //$this->relationCount($comment, 'replys');
        return $replys;
    }
    /**
     * 修改评论信息
     * @param  [type] $comment_info [description]
     * @param  [type] $comment_id   [description]
     * @return [type]               [description]
     */
    public function modify($comment_info, $comment_id = null)
    {
        $comment_id = $comment_id ? : $comment_info['id'];
        $this->commentUpdate($comment_info, $comment_id);
    }
    public function del($comment_id)
    {
        return $this->commentDestroy($comment_id);
    }
    public function lists()
    {
        return $this->withCount(['replys'=>function($query){$query->where('reply_id = 1');}])->find(1);
    }
    public function replyLists($comment)
    {
        return $comment->replys()->select();
    }
}
